package com.imooc.course4

/*
 * @Author Wangjin
 * @Date 2019-02-24
 * @Email wangjin4901@vip.qq.com
 */

object FunctionApp {
  def main(args: Array[String]): Unit = {
//    println(add(2, 3))
//    println(three())
//    println(three) // 没有入参的函数,调用时括号可以省略的
//    sayHello()
//    sayHello
//    sayHello("Wangjin")
//    sayName()
//    sayName("wangli")
//    loadConf("wj.properties")
//    loadConf()
//    println(speed(100,10))
//    println(speed(distance = 100, time = 10))
//    println(speed(time = 10, distance = 100))
//    println(sum(2, 3))
//    println(sum2(1,2,3,4,5,6,7))
//    println(Range(1,10))
//    println(1.until(10))
    for (i <- 1 to 10 if i % 2 == 0) {
      println(i)
    }

    val arr = Array("Hadoop", "Spark SQL", "Spark Streaming", "Storm", "Scala")
    for (item <- arr) {
      println(item)
    }

    arr.foreach(item => println(item))

    var (num, sum) = (100, 0)
    while (num > 0) {
      sum += num
      num = num - 1
    }
    println(sum)
  }

  def add(x:Int, y: Int): Int = {
    x + y // 最后一行就是返回值 不需要return
  }

  def three() = 1 + 2

  def sayHello(): Unit = {
    println("say Hello...")
  }

  def sayHello(name: String): Unit = {
    println("say Hello..." + name)
  }

  def sayName(name:String = "WJ"): Unit = {
    println(name)
  }

  def loadConf(conf:String = "default.properties"): Unit = {
    println(conf)
  }

  def speed(distance: Float, time: Float): Float = {
    distance / time
  }

  def sum (a:Int, b:Int): Int = {
    a + b
  }

  def sum2(numbers:Int*) = {
    var result  = 0 ;
    for (number <- numbers) {
      result += number
    }
    result
  }
}
